/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogRuleEdit.java
 *
 * Created on 26.04.2010, 20:32:01
 */

package shaman.storage;

import java.io.IOException;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;
import shaman.FileListForm;
import shaman.dialog.selectfolder.FolderSelect;

/**
 *
 * @author Shaman
 */
public class DialogRuleEdit extends javax.swing.JDialog {

    Rule rule = null;

    /** Creates new form DialogRuleEdit */
    public DialogRuleEdit(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    private void loadRuleValues()
    {
        jShowInSubfolders.setSelected(rule.showInSubfolders());
        jAddable.setSelected(rule.isAddable());
        //Тип правила
        if (rule.getRuleType() == Rule.RuleTypes.MASK)
            RuleTypeGroup.setSelected(radioButtonMask.getModel(), true);
        else
            RuleTypeGroup.setSelected(radioButtonRegexp.getModel(), true);
        jValueText.setText(rule.getValue());
        jDirectoryText.setText(rule.getDirectory());
        jCommentText.setText(rule.getComment());
    }
    
    private void saveRuleValue()
    {
        rule.setShowInSubfolders(jShowInSubfolders.isSelected());
        rule.setAddable(jAddable.isSelected());
        if (RuleTypeGroup.isSelected(radioButtonMask.getModel()))
            rule.setRuleType(Rule.RuleTypes.MASK);
        if (RuleTypeGroup.isSelected(radioButtonRegexp.getModel()))
            rule.setRuleType(Rule.RuleTypes.REGEXP);
        rule.setValue(jValueText.getText());
        rule.setDirectory(jDirectoryText.getText());
        rule.setComment(jCommentText.getText());
    }
    
    public DialogRuleEdit(java.awt.Frame parent, boolean modal, Rule rule) {
        super(parent, modal);
        initComponents();
        this.rule = rule;
        loadRuleValues();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        RuleTypeGroup = new javax.swing.ButtonGroup();
        radioButtonMask = new javax.swing.JRadioButton();
        radioButtonRegexp = new javax.swing.JRadioButton();
        jValueLabel = new javax.swing.JLabel();
        jValueText = new javax.swing.JTextField();
        jCommentText = new javax.swing.JTextField();
        jCommentLabel = new javax.swing.JLabel();
        jDirectoryText = new javax.swing.JTextField();
        jSelectDirectory = new javax.swing.JButton();
        jClose = new javax.swing.JButton();
        jOK = new javax.swing.JButton();
        jAddable = new javax.swing.JCheckBox();
        jbRestore = new javax.swing.JButton();
        jbShowFileList = new javax.swing.JButton();
        jShowInSubfolders = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(shaman.RegBackupApp.class).getContext().getResourceMap(DialogRuleEdit.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setAlwaysOnTop(true);
        setModal(true);
        setName("Form"); // NOI18N

        RuleTypeGroup.add(radioButtonMask);
        radioButtonMask.setText(resourceMap.getString("radioButtonMask.text")); // NOI18N
        radioButtonMask.setName("radioButtonMask"); // NOI18N

        RuleTypeGroup.add(radioButtonRegexp);
        radioButtonRegexp.setText(resourceMap.getString("radioButtonRegexp.text")); // NOI18N
        radioButtonRegexp.setName("radioButtonRegexp"); // NOI18N

        jValueLabel.setText(resourceMap.getString("jValueLabel.text")); // NOI18N
        jValueLabel.setName("jValueLabel"); // NOI18N

        jValueText.setText(resourceMap.getString("jValueText.text")); // NOI18N
        jValueText.setName("jValueText"); // NOI18N

        jCommentText.setText(resourceMap.getString("jCommentText.text")); // NOI18N
        jCommentText.setName("jCommentText"); // NOI18N

        jCommentLabel.setText(resourceMap.getString("jCommentLabel.text")); // NOI18N
        jCommentLabel.setName("jCommentLabel"); // NOI18N

        jDirectoryText.setText(resourceMap.getString("jDirectoryText.text")); // NOI18N
        jDirectoryText.setName("jDirectoryText"); // NOI18N

        jSelectDirectory.setText(resourceMap.getString("jSelectDirectory.text")); // NOI18N
        jSelectDirectory.setName("jSelectDirectory"); // NOI18N
        jSelectDirectory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSelectDirectoryActionPerformed(evt);
            }
        });

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(shaman.RegBackupApp.class).getContext().getActionMap(DialogRuleEdit.class, this);
        jClose.setAction(actionMap.get("jCancelClick")); // NOI18N
        jClose.setText(resourceMap.getString("jClose.text")); // NOI18N
        jClose.setName("jClose"); // NOI18N

        jOK.setAction(actionMap.get("jOKClick")); // NOI18N
        jOK.setText(resourceMap.getString("jOK.text")); // NOI18N
        jOK.setName("jOK"); // NOI18N

        jAddable.setText(resourceMap.getString("jAddable.text")); // NOI18N
        jAddable.setName("jAddable"); // NOI18N

        jbRestore.setText(resourceMap.getString("jbRestore.text")); // NOI18N
        jbRestore.setActionCommand(resourceMap.getString("jbRestore.actionCommand")); // NOI18N
        jbRestore.setName("jbRestore"); // NOI18N
        jbRestore.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbRestoreClick(evt);
            }
        });

        jbShowFileList.setText(resourceMap.getString("jbShowFileList.text")); // NOI18N
        jbShowFileList.setToolTipText(resourceMap.getString("jbShowFileList.toolTipText")); // NOI18N
        jbShowFileList.setName("jbShowFileList"); // NOI18N
        jbShowFileList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbShowFileListActionPerformed(evt);
            }
        });

        jShowInSubfolders.setText(resourceMap.getString("jShowInSubfolders.text")); // NOI18N
        jShowInSubfolders.setActionCommand(resourceMap.getString("jShowInSubfolders.actionCommand")); // NOI18N
        jShowInSubfolders.setName("jShowInSubfolders"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jAddable)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jShowInSubfolders)
                        .addGap(289, 289, 289))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(radioButtonMask)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(radioButtonRegexp))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jValueLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jCommentLabel)
                                    .addComponent(jSelectDirectory, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jDirectoryText, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE)
                                    .addComponent(jCommentText, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE)
                                    .addComponent(jValueText, javax.swing.GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jbRestore, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbShowFileList)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 144, Short.MAX_VALUE)
                                .addComponent(jOK)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jClose)))
                        .addGap(14, 14, 14))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(10, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jAddable)
                    .addComponent(jShowInSubfolders))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(radioButtonMask)
                    .addComponent(radioButtonRegexp))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jValueLabel)
                    .addComponent(jValueText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jDirectoryText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSelectDirectory))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCommentText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCommentLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jClose)
                    .addComponent(jOK)
                    .addComponent(jbRestore)
                    .addComponent(jbShowFileList))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbRestoreClick(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbRestoreClick
        loadRuleValues();
    }//GEN-LAST:event_jbRestoreClick

    private void jbShowFileListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbShowFileListActionPerformed
        saveRuleValue();
        try {
            FileListForm flf = new FileListForm(rule.getFileList(), false, "Список файлов соответсвующих правилу: " + jValueText.getText(), "Список файлов");
            flf.setLocationByPlatform(true);
            flf.setVisible(true);
            this.setVisible(false);
        } catch (IOException ex) {
            JOptionPane.showConfirmDialog(this, ex.getLocalizedMessage(), "ERROR", JOptionPane.OK_OPTION, JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jbShowFileListActionPerformed

    private void jSelectDirectoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSelectDirectoryActionPerformed
        String path = FolderSelect.show(null, "Выберите папку для архивации");
        if (path != null)
            jDirectoryText.setText(path);
    }//GEN-LAST:event_jSelectDirectoryActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DialogRuleEdit dialog = new DialogRuleEdit(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    @Action
    public void jCancelClick() {
        this.setVisible(false);
    }

    @Action
    public void jOKClick() {
        saveRuleValue();
        this.setVisible(false);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup RuleTypeGroup;
    private javax.swing.JCheckBox jAddable;
    private javax.swing.JButton jClose;
    private javax.swing.JLabel jCommentLabel;
    javax.swing.JTextField jCommentText;
    private javax.swing.JTextField jDirectoryText;
    private javax.swing.JButton jOK;
    private javax.swing.JButton jSelectDirectory;
    private javax.swing.JCheckBox jShowInSubfolders;
    private javax.swing.JLabel jValueLabel;
    private javax.swing.JTextField jValueText;
    private javax.swing.JButton jbRestore;
    private javax.swing.JButton jbShowFileList;
    private javax.swing.JRadioButton radioButtonMask;
    private javax.swing.JRadioButton radioButtonRegexp;
    // End of variables declaration//GEN-END:variables

}
